const {
  $util,
} = getApp();
const date = new Date();
const yearsColumn = [];

for (let i = date.getFullYear(); i >= 2000; i--) {
  yearsColumn.push(i);
}

Page({
  data: {
    list: [],
    page: 1,
    isloadmore: true,
    keyword: '',
    year: '',
    imgUrl: $util.getImgUrl(),
    showPicker: false,
    yearsColumn: yearsColumn,
    yearArr: [0],
    ctype: 0, // 0 我的  1 管理
    adminNav: [{
      title: '我提交的',
      id: 0,
    }, {
      title: '待我审批',
      id: 1,
    }],
  },
  onLoad(ops) {
    let year = new Date().getFullYear();
    this.setData({
      year: year,
      nowDate: year,
    });
    wx.setNavigationBarTitle({
      title: '党员承诺',
    });
    Number(ops.ctype || 0) === 1 && this.changeNav({
      detail: { obj: { id: 1 }}, 
    });
  },
  onShow() {
    this.setData({
      page: 1,
    }, () => {
      Number(this.data.ctype) === 1 ? this.loadManageList() : this.loadList();
    });
  },
  bindSortChange(e) {
    console.log(e);
    this.setData({
      chooseYear: this.data.yearsColumn[e.detail.value],
      yearArr: e.detail.value,
    });
  },
  showPicker() {
    this.setData({
      showPicker: true,
    });
  },
  hidePicker() {
    this.setData({
      showPicker: false,
    });
  },
  loadList() {
    let { page, year, keyword } = this.data;
    let param = {
      page: page,
      keyword: keyword,
    };
    '' !== year && (param.year = year);
    $util.request({
      url: 'entry/wxapp/partyman.promisenew.getPromiseList',
      data: param,
      showLoading: true,
      success: (res) => {
        let list = res.list;
        if (page > 1) {
          list = [...this.data.list, ...list];
        }
        this.setData({
          list,
          canshow: res.canshow,
          isloadmore: res.list.length > 0 ? true : false,
          page: res.list.length > 0 ? page + 1 : page,
          isLoad: true,
          navLoad: true,
        });
      },
    });
  },

  /* 管理党员承诺 */
  loadManageList() {
    let {
      page,
      list,
    } = this.data;
    $util.request({
      url: 'entry/wxapp/verify.promisenew.getList',
      data: {
        check: 0,
        page: page,
      },
      success: (res) => {
        let lists = res.list;
        if (page > 1) {
          lists = [...list, ...lists];
        }
        this.setData({
          isloadmore: res.list.length !== 0,
          page: res.list.length !== 0 ? page + 1 : page,
          list: lists,
          isLoad: true,
          navLoad: true,
          canshow: true,
        });
      },
      fail: (err) => {
        console.error(err);
      },
    });
  },
  bindIpVal(e) {
    this.setData({
      keyword: e.detail.value,
    });
  },
  search() {
      this.setData({
        page: 1,
      }, () => {
        this.loadList();
      });
  },
  bindYearChange() {
    this.hidePicker();
    this.setData({
      year: this.data.chooseYear,
      page: 1,
    }, () => {
      this.loadList();
    });
  },
  onReachBottom() {
    let { isloadmore, ctype } = this.data;
    if (!isloadmore) return;
    Number(ctype) === 0 ? this.loadList() : this.loadManageList();
  },

  changeNav(e) {
    this.setData({
      page: 1,
      isloadmore: true,
      ctype: Number(e.detail.obj.id),
      isLoad: false,
      list: [],
    }, () => {
      if (this.data.canshow && e.detail.obj.id === 0) {
        this.loadList();
      } else {
        this.loadManageList();
      }
    });
  },
});
